﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessSubsidyPayment
// 文件功能描述：补贴发放数据访问层
//
// 
// 创建标识：2012-05-24 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessSubsidyPayment
    {
        /// <summary>
        /// 已发所有已发卡且状态为正常的卡的补贴卡的数量
        /// </summary>
        /// <returns></returns>
        public int GetSubsidyPaymentCount()
        {
            sqldb db = new sqldb();
            string query = "select count(1) from SubsidyPaymen_View";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 已发所有已发卡且状态为正常的卡的补贴卡的数量
        /// </summary>
        /// <returns></returns>
        public int GetSubsidyPaymentCount(string where)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from SubsidyPaymen_View where 1=1 " + where;
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public DataSet GetdeptName()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select Dep_ID,deptName from DeptView order by Dep_ID";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 获取所有已发卡且状态为正常的卡的补贴卡的信息
        /// </summary>
        /// <returns></returns>
        public DataSet SelSubsidyPaymentInfo(int pagesize, int startindex, string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY CardNo ASC) rownumber,* " +
                               "from SubsidyPaymen_View where 1=1 " + strquery + ") B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        public DataSet SelLeftBox(string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select * from SubsidyPaymen_View where 1=1 " + strquery;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 保存（补贴发放）
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int SaveSubgidyPayment(object[] obj, string[] sempall)
        {
            sqldb db = new sqldb();
            int count = 0;
            for (int i = 0; i < sempall.Length; i++)
            {
                SqlParameter[] parameters = {
                    new SqlParameter("@EMPNO", SqlDbType.VarChar, 20),
                    new SqlParameter("@OPERATOR_VALUE",SqlDbType.Int),
                    new SqlParameter("@BalanceLimit", SqlDbType.Int),
                    new SqlParameter("@OPERATORID", SqlDbType.VarChar,20),
					new SqlParameter("@RETURN",SqlDbType.VarChar,20)
                    };
                parameters[0].Value = sempall[i];
                parameters[1].Value = obj[1];
                parameters[2].Value = obj[0];
                parameters[3].Value = obj[2];
                parameters[4].Direction = ParameterDirection.Output;
                int icount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.StoredProcedure, "SP_AddSubgidyMoney", parameters);
                if (icount != 0)
                {
                    count++;
                }
            }
            return count;
        }
    }
}
